<template>
    <div>
        <div>
            <el-form :inline="true" label-width="100" :model="formInline"  class="demo-form-inline">
                <el-form-item label="托盘编码">
                    <el-input class="input-width" v-model="formInline.palletCode" size="mini" placeholder="交接单号"></el-input>
                </el-form-item>
                <el-form-item label="工作包编码">
                    <el-input class="input-width" v-model="formInline.packageCode" size="mini" placeholder="条码"></el-input>
                </el-form-item>
                <el-form-item label="AO编码">
                    <el-input class="input-width" v-model="formInline.aoCode" size="mini" placeholder="物料图号"></el-input>
                </el-form-item>
                <el-form-item label="架次">
                    <el-input class="input-width" v-model="formInline.startPlaneNo" size="mini" placeholder="成品型号"></el-input>
                </el-form-item>
                <el-form-item label="配送时间">
                    <el-date-picker style="width:140px" value-format="yyyy-MM-dd" v-model="formInline.startTime" size="mini" type="date" placeholder="开始日期"></el-date-picker>到
                    <el-date-picker style="width:140px" value-format="yyyy-MM-dd" v-model="formInline.endTime" size="mini" type="date" placeholder="结束日期"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="mini" @click="doQuery">查询</el-button>
                    <el-button type="primary" size="mini" @click="doReset">重置</el-button>
                    <el-button type="primary" size="mini" @click="reDelivery">重新配送</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table ref="multipleTable" height="550" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="40"> </el-table-column>
                <el-table-column type="index" label="序号" width="50" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="mesCheckQualiticationId" label="合格证" v-if="false" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="mProductNumber" label="型号" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="carCode" label="小车码" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="toolCode" label="托盘码" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="requiredTime" label="日计划时间" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="aoNo" label="AO编码" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="itemCode" label="图号" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="qualityCode" label="质量编号" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="deliveryQty" label="配送数量" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="groupCode" label="组号" v-if="false" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="barcode" label="条码号" width="150" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="productCode" label="机型" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="batchNo" label="批次" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="startLotNo" label="起始架次" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="endLotNo" label="截止架次" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="erpWmsDeliveryPlanId" label="ERP交接单ID" v-if="false" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="infoId" label="配套信息ID" v-if="false" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="wdpId" label="交接单ID" v-if="false" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="qualityId" label="质量ID" v-if="false" :show-overflow-tooltip="true"> </el-table-column>
            </el-table>
            <div>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.currentPage"
                :page-sizes="[50,100, 200, 300, 400]" :page-size="page.pageSize"  layout="total, sizes, prev, pager, next" :total="page.total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import request from "@/utils/request.js";
export default{
    name:"wmsDeliveryQuery",
    data(){
        return {
            formInline:{
                palletCode:"",
                packageCode:"",
                aoCode:"",
                startPlaneNo:"",
                startTime:"",
                endTime:""
            },
            page:{
                total:1,
                pageSize:50,
                currentPage:1
            },
            tableData:[],
            selectData:[],
        }
    },
    methods:{
        handleSelectionChange(val){
            for (let index = 0; index < val.length; index++) {
                this.selectData.push(val[index].groupCode)   
            }
            // 去重
            let uniqueArr = Array.from(new Set(this.selectData));
            this.selectData = uniqueArr
        },
        handleSizeChange(val){
            this.page.pageSize = val;
        },
        handleCurrentChange(val){
            this.page.currentPage = val;
            this.$options.methods.doQuery.bind(this)();
        },
        //查询
        doQuery(){
            var that = this;
            request.post("/MESLogResend/searchDispatchedData",{"page":this.page,"queryParam":this.formInline}).then((res)=>{
                console.log(res);
                that.tableData = res.data.list;
                that.page.total = res.data.total;
            });
        },
        //重置
        doReset(){
            Object.assign(this.formInline,this.$options.data().formInline);
        },
        //重新配送
        reDelivery(){
            if (this.selectData.length === 0) {
                this.$Alert({
                    title: "失败",
                    message: "请至少勾选一条数据",
                    type: "error",
                });
            }
            // 调后端接口
            var failCnt = 0;
            var failDesc = "";
            var successDesc = "";
            for (let index = 0; index < this.selectData.length; index++) {
                console.log("第【" + index + "】次数据");
                console.log(this.selectData[index]);
                request.post("/MESLogResend/reSendToMES",{"groupCode":this.selectData[index]}).then((res)=>{
                    if (res.data.retCode != '200') {
                        failCnt++;
                        failDesc = res.data.message;
                    }else{
                        successDesc = res.data.message;
                    }
                });
            }
            if (failCnt < 1) {
                this.$Alert({
                    title: "成功",
                    message: successDesc,
                    type: "success",
                });
                this.selectData = [];
                // 接口返回200后 重制selectData 并重新执行 doQuery;
                this.doQuery();
            }else{
                this.$Alert({
                    title: "失败",
                    message: failDesc,
                    type: "error",
                });
            }
        },
    }
}
</script>
